package com.osgix.common.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;

public class _JsonUtils {

	@SuppressWarnings("unchecked")
	public static <T> T getBean(String jsonString, Class<T> requestType) {
		JSONObject jsonObject = JSONObject.fromObject(jsonString);
		T t = (T) JSONObject.toBean(jsonObject, requestType);
		return t;
	}

	public static String getJson(Object obj) {
		JSONObject json = JSONObject.fromObject(obj);
		return json.toString();
	}

	public static String getArrayJson(Object list) {
		JSONArray jsonArray = JSONArray.fromObject(list);
		return jsonArray.toString();
	}

	/**
	 * 转换对象为json对象 Dec 3, 2009,3:50:23 AM
	 * 
	 * @author Azheng
	 * @param obj
	 *            转换对象
	 * @param incNull
	 *            是否包含null值属性
	 * @return json 对象 string
	 */
	public static String getObjectJson(Object obj, boolean incNull) {
		JSONObject json;
		if (incNull) {
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
				public boolean apply(Object source, String name, Object value) {
					if (value != null) {
						return false;
					}
					return true;
				}
			});
			json = JSONObject.fromObject(obj, jsonConfig);
		} else {
			json = JSONObject.fromObject(obj);
		}
		return json.toString();
	}

	/**
	 * 转换集合对象为json数组 Dec 3, 2009,3:50:23 AM
	 * 
	 * @author Azheng
	 * @param obj
	 *            转换对象
	 * @param incNull
	 *            是否包含null值属性
	 * @return json 数组 string
	 */
	public static String getArrayJson(Object obj, boolean incNull) {
		JSONArray json;
		if (incNull) {
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
				public boolean apply(Object source, String name, Object value) {
					if (value != null) {
						return false;
					}
					return true;
				}
			});
			json = JSONArray.fromObject(obj, jsonConfig);
		} else {
			json = JSONArray.fromObject(obj);
		}
		return json.toString();
	}

	/**
	 * 转换集合对象为json数组 Dec 3, 2009,3:50:23 AM
	 * 
	 * @author Azheng
	 * @param obj
	 *            转换对象
	 * @param excludeProperties
	 *            将排除的属性名用","分隔
	 * @return json 数组 string
	 */
	public static String getArrayJson(Object obj, final String excludeProperties) {
		JSONArray json;
		if (excludeProperties != null && !"".equals(excludeProperties)) {
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
				public boolean apply(Object source, String name, Object value) {
					String[] proArr = excludeProperties.split(",");
					for (int i = 0; i < proArr.length; i++) {
						if (name.equals(proArr[i]))
							return true;
					}
					return false;
				}
			});
			json = JSONArray.fromObject(obj, jsonConfig);
		} else {
			json = JSONArray.fromObject(obj);
		}
		return json.toString();
	}

	@SuppressWarnings("unchecked")
	public static String getJstreeJson(List list) {
		List tree = new ArrayList();
		for (Object obj : list) {
			Map jstreeMap = new HashMap();
			Map map = (Map) obj;
			Map attributes = new HashMap();
			attributes.put("id", map.get("id"));
			attributes.put("key", map.get("key"));
			jstreeMap.put("state", "closed");
			jstreeMap.put("data", map);
			jstreeMap.put("attributes", attributes);
			tree.add(jstreeMap);
		}
		return getArrayJson(tree);
	}

}
